/*
 * @Description:
 * @Version: 1.0
 * @Autor: lzc
 * @Date: 2021-06-25 09:36:12
 * @LastEditors: lzc
 * @LastEditTime: 2021-06-28 11:08:30
 */
#include "my_elcdif.h"
#include "rtconfig.h"
#include <rtdevice.h>
#include "drv_lcd.h"
#include "fsl_common.h"
#include "fsl_iomuxc.h"
#include "fsl_elcdif.h"
#include "drv_gpio.h"

rt_device_t lcd_dev = RT_NULL;
struct rt_device_graphic_info sys_lcd_info ;


void my_lcd_init(void)
{
    lcd_dev = rt_device_find("lcd");
    ELCDIF_PinInit();
    rt_device_init(lcd_dev); 
    lcd_dev->control(lcd_dev, RTGRAPHIC_CTRL_GET_INFO, (void *)&sys_lcd_info);
    ELCDIF_ClockInit(33, 4, 6, 1);                          //设置LCD时钟为33Mhz
}

//LCD时钟设置,LCD CLK=24Mhz*loopdiv/postdiv/lcdif1prediv/lcdif1div
//如要设置LCD CLK=9.3Mhz，那么LCD CLK=24*31/8/5/2=9.3Mhz
//loopdiv: PLL5(VIDEO PLL)的loop Divider，可选范围27~54
//postdiv: PLL5(VIDEO PLL)的post Divider，可选范围1，2，4，8
//lcdif1prediv：LCDIF PRE分频值，可选范围1~8
//lcdif1div：   LCDIF 分频值，可选范围1~8
void ELCDIF_ClockInit(u8 loopdiv, u8 postdiv, u8 lcdif1prediv, u8 lcdif1div)
{
    clock_video_pll_config_t video_config;

    video_config.src = kCLOCK_PllClkSrc24M;
    video_config.loopDivider = loopdiv;
    video_config.postDivider = postdiv;
    video_config.numerator = 0;
    video_config.denominator = 0;

    //配置Video PLL(PLL5)
    CLOCK_InitVideoPll(&video_config);

    //设置eLCDIF Pre时钟源，这里选择PLL5(Video PLL)作为eLCDIF时钟源，可选择范围：
    //  0     PLL2
    //  1     PLL3 PFD3
    //  2     PLL5
    //  3     PLL2 PFD0
    //  4     PLL2 PFD1
    //  5     PLL3 PFD1
    CLOCK_SetMux(kCLOCK_LcdifPreMux, 2);
    CLOCK_SetDiv(kCLOCK_LcdifPreDiv, lcdif1prediv - 1); //LCDIF_PRE分频值设置,可设置值:0~7，对应为1~8分频
    CLOCK_SetDiv(kCLOCK_LcdifDiv, lcdif1div - 1);      //LCDIF分频值设置，可设置值:0~7,对应为1~8分频，
}

//RGB LCD引脚初始化
void ELCDIF_PinInit(void)
{

    //配置RGB LCD所使用的IO的复用功能
    IOMUXC_SetPinMux(IOMUXC_GPIO_B0_00_LCD_CLK, 0);
    IOMUXC_SetPinMux(IOMUXC_GPIO_B0_01_LCD_ENABLE, 0);
    IOMUXC_SetPinMux(IOMUXC_GPIO_B0_02_LCD_HSYNC, 0);
    IOMUXC_SetPinMux(IOMUXC_GPIO_B0_03_LCD_VSYNC, 0);
    IOMUXC_SetPinMux(IOMUXC_GPIO_B0_04_LCD_DATA00, 0);
    IOMUXC_SetPinMux(IOMUXC_GPIO_B0_05_LCD_DATA01, 0);
    IOMUXC_SetPinMux(IOMUXC_GPIO_B0_06_LCD_DATA02, 0);
    IOMUXC_SetPinMux(IOMUXC_GPIO_B0_07_LCD_DATA03, 0);
    IOMUXC_SetPinMux(IOMUXC_GPIO_B0_08_LCD_DATA04, 0);
    IOMUXC_SetPinMux(IOMUXC_GPIO_B0_09_LCD_DATA05, 0);
    IOMUXC_SetPinMux(IOMUXC_GPIO_B0_10_LCD_DATA06, 0);
    IOMUXC_SetPinMux(IOMUXC_GPIO_B0_11_LCD_DATA07, 0);
    IOMUXC_SetPinMux(IOMUXC_GPIO_B0_12_LCD_DATA08, 0);
    IOMUXC_SetPinMux(IOMUXC_GPIO_B0_13_LCD_DATA09, 0);
    IOMUXC_SetPinMux(IOMUXC_GPIO_B0_14_LCD_DATA10, 0);
    IOMUXC_SetPinMux(IOMUXC_GPIO_B0_15_LCD_DATA11, 0);
    IOMUXC_SetPinMux(IOMUXC_GPIO_B1_00_LCD_DATA12, 0);
    IOMUXC_SetPinMux(IOMUXC_GPIO_B1_01_LCD_DATA13, 0);
    IOMUXC_SetPinMux(IOMUXC_GPIO_B1_02_LCD_DATA14, 0);
    IOMUXC_SetPinMux(IOMUXC_GPIO_B1_03_LCD_DATA15, 0);

    IOMUXC_SetPinMux(IOMUXC_GPIO_SD_B1_02_GPIO3_IO02, 0);      //LCD背光使能引脚

    //配置RGB LCD所使用的IO的详细功能
    //低转换速度,驱动能力为R0/2,速度为100Mhz，关闭开路功能，使能pull/keepr
    //选择pull功能，上拉100K Ohm，是能Hyst
    IOMUXC_SetPinConfig(IOMUXC_GPIO_B0_00_LCD_CLK, 0x1B090);
    IOMUXC_SetPinConfig(IOMUXC_GPIO_B0_01_LCD_ENABLE, 0x1B090);
    IOMUXC_SetPinConfig(IOMUXC_GPIO_B0_02_LCD_HSYNC, 0x1B090);
    IOMUXC_SetPinConfig(IOMUXC_GPIO_B0_03_LCD_VSYNC, 0x1B090);
    IOMUXC_SetPinConfig(IOMUXC_GPIO_B0_04_LCD_DATA00, 0x1B090);
    IOMUXC_SetPinConfig(IOMUXC_GPIO_B0_05_LCD_DATA01, 0x1B090);
    IOMUXC_SetPinConfig(IOMUXC_GPIO_B0_06_LCD_DATA02, 0x1B090);
    IOMUXC_SetPinConfig(IOMUXC_GPIO_B0_07_LCD_DATA03, 0x1B090);
    IOMUXC_SetPinConfig(IOMUXC_GPIO_B0_08_LCD_DATA04, 0x1B090);
    IOMUXC_SetPinConfig(IOMUXC_GPIO_B0_09_LCD_DATA05, 0x1B090);
    IOMUXC_SetPinConfig(IOMUXC_GPIO_B0_10_LCD_DATA06, 0x1B090);
    IOMUXC_SetPinConfig(IOMUXC_GPIO_B0_11_LCD_DATA07, 0x1B090);
    IOMUXC_SetPinConfig(IOMUXC_GPIO_B0_12_LCD_DATA08, 0x1B090);
    IOMUXC_SetPinConfig(IOMUXC_GPIO_B0_13_LCD_DATA09, 0x1B090);
    IOMUXC_SetPinConfig(IOMUXC_GPIO_B0_14_LCD_DATA10, 0x1B090);
    IOMUXC_SetPinConfig(IOMUXC_GPIO_B0_15_LCD_DATA11, 0x1B090);
    IOMUXC_SetPinConfig(IOMUXC_GPIO_B1_00_LCD_DATA12, 0x1B090);
    IOMUXC_SetPinConfig(IOMUXC_GPIO_B1_01_LCD_DATA13, 0x1B090);
    IOMUXC_SetPinConfig(IOMUXC_GPIO_B1_02_LCD_DATA14, 0x1B090);
    IOMUXC_SetPinConfig(IOMUXC_GPIO_B1_03_LCD_DATA15, 0x1B090);


}



inline void ELCDIF_Draw_Point(u16 x, u16 y, u32 color)
{
    lcd_dev = rt_device_find("lcd");
    lcd_dev->control(lcd_dev, RTGRAPHIC_CTRL_GET_INFO, (void *)&sys_lcd_info);
    *(u32 *)((u32)sys_lcd_info.framebuffer +
             (sys_lcd_info.pixel_format / 8) *
             (sys_lcd_info.width * (sys_lcd_info.height - x - 1) + y)) = color;
	lcd_dev->user_data = (void *)&sys_lcd_info;
}

void ELCDIF_Clear(u32 color)
{
    lcd_dev = rt_device_find("lcd");
    lcd_dev->control(lcd_dev, RTGRAPHIC_CTRL_GET_INFO, (void *)&sys_lcd_info);
    rt_kprintf("%x\r\n", sys_lcd_info.framebuffer);
    memset(sys_lcd_info.framebuffer, color, (sys_lcd_info.bits_per_pixel / 8) * (sys_lcd_info.width * sys_lcd_info.height));
    lcd_dev->user_data = (void *)&sys_lcd_info;
}
